﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeetCodeFly.studyPlan.programming.day03
{
    public static class DayThree
    {
        public static int LargestPerimeter(int[] nums)
        {
            // 两条边之和大于第三边 ，排序循环默认最后一个为最大边,满足循环时有三条边存在
            Array.Sort(nums);
            int sum = 0;
            for (int i = nums.Length - 1; i >= 2; i--)
            {
                if (nums[i] < nums[i - 1] + nums[i - 2])
                {
                    sum = nums[i] + nums[i - 1] + nums[i - 2];
                    break;
                }
            }
            return sum;
        }

        public static int NearestValidPoint(int x, int y, int[][] points)
        {
            int minIndex = -1; // 默认坐标
            int minTemp = -1; // 最小值
            for (int i = 0; i < points.Length; i++)
            {
                if (x != points[i][0] && y!= points[i][1])
                {
                    continue;
                }
                if (x == points[i][0] || y == points[i][1])
                {
                    int temp = Math.Abs(x - points[i][0]) + Math.Abs(y - points[i][1]);
                    if (minIndex == -1) // 第一次
                    {
                        minIndex = i;
                        minTemp = temp;
                    }
                    if (minTemp>temp)
                    {
                        minIndex = i;
                        minTemp = temp;
                    }
                }
            }
            return minIndex;
        }
    }
}
